
package com.amado.springboot.springbootmybatish2.service.impl;

import com.amado.springboot.springbootmybatish2.domain.User;
import com.amado.springboot.springbootmybatish2.domain.UserMoney;
import com.amado.springboot.springbootmybatish2.mapper.UserMapper;
import com.amado.springboot.springbootmybatish2.service.UserMoneyService;
import com.amado.springboot.springbootmybatish2.service.UserService;
import com.amado.springboot.springbootmybatish2.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author wangyl-910
 * @description
 * @date 2017/11/25
 * @see
 */
@Component
public class UserServiceImpl implements UserService {


    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserMoneyService userMoneyService;

    @Transactional(readOnly = true)
    @Override
    public User getUser(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public UserVO addUser(UserVO user) {
        userMapper.insertSelective(user);
        addUserMoney(user);
        return user;
    }

    private void addUserMoney(UserVO user) {
        UserMoney userMoney = user.getUserMoney();
        userMoney.setUserId(user.getId());
        userMoneyService.adUserMoney(userMoney);
    }
}
